using UnityEngine;
using System.Collections.Generic;

public class Scenery : Interactive
{
	[Header("Scenery")]
	public List<Vector3> outsidePositions = new List<Vector3>();
	public List<Vector3> insidePositions = new List<Vector3>();

	protected override void Start ()
	{
		base.Start ();
		Init(null);
	}

	private void Reset()
	{
		gameObject.layer = LayerMask.NameToLayer("Scenery");
	}

	public override void RecalculateAreas()
	{
		List<Tile> areaIn = new List<Tile>();
		List<Tile> areaOut = new List<Tile>();
		
		// Translate vector list to tile list
		foreach (Vector3 outside in outsidePositions)
		{
			areaOut.Add(FloorManager.Instance.GetTile(PosToWorld(outside)));
		}
		foreach (Vector3 inside in insidePositions)
		{
			areaIn.Add(FloorManager.Instance.GetTile(PosToWorld(inside)));
		}

		selectArea.Clear();
		foreach (Tile tileIn in areaIn)
		{
			if (tileIn == null)
			{
				continue;
			}

			foreach (Tile tileOut in areaOut)
			{
				if (tileIn.IsNeighbour(tileOut) && !selectArea.Contains(tileOut))
				{
					selectArea.Add(tileOut);
				}
			}
		}
	}

	public Vector3 PosToWorld(Vector3 _pos)
	{
		return transform.rotation * _pos + transform.position;
	}

	public Vector3 WorldToPos(Vector3 _world)
	{
		return Quaternion.Inverse(transform.rotation) * (_world - transform.position);
	}

	protected override void OnDrawGizmosSelected()
	{
		base.OnDrawGizmosSelected();

		if (!UnityEditor.EditorApplication.isPlaying)
		{
			foreach (Vector3 pos in outsidePositions)
			{
				Gizmos.DrawIcon(PosToWorld(pos), "Scenery/gizmoAreaOutside");
			}
			foreach (Vector3 pos in insidePositions)
			{
				Gizmos.DrawIcon(PosToWorld(pos), "Scenery/gizmoAreaInside");
			}
		}
	}
}
